/**
* @fileoverview HQ dom 对象，包含对浏览器dom的一些操作
 * @version 1.$Rev: 564 $
 * @author zhang jianbin
 * @lastUpdate $Date: 2008-07-09 12:05:32 +0800 (星期三, 09 七月 2008) $
 */
/**
 * dom 对象处理
 * @namespace HQ.dom
 */
	HQ.dom = {
		/**
		 * 根据id获取dom对象
		 * @param {string} id 对象ID号
		 * @return Object
		 */
		getById:function(id){
			return document.getElementById(id);
		},
		
		/**
		 * 根据name获取dom集合，有些标签例如li、span无法通过getElementsByName拿到，加上tagName指明就可以
		 * 	<li name="1">fdsafda</li><span name="1">fdsa</span>  ie只能获取到li，非ie下两者都可以
		 * @param {string} name 名称
		 * @param {string} tagName 标签名称,
		 * @return Array
		 */
		getByName: function(name, tagName){
			if(!tagName)
				return document.getElementsByName(name);

			var arr = [];
			var e = document.getElementsByTagName(tagName);
			for(var i=0; i<e.length; ++i) {
				if(!!e[i].getAttribute("name") && (e[i].getAttribute("name").toLowerCase()==name.toLowerCase())) {
					arr.push(e[i]);
				}
			}
	
			return arr;
		},
		
		/**
		 * 获得对象
		 * @param {String|HTMLElement} e 包括id号，或则Html Element对象
		 */
		get: function(e){
			if(e && (e.tagName || e.item)){
				return e;
			}
			
			return this.getById(e);
		},
		/**
		 * 获得节点
		 * @param {String|HTMLElement} el 
		 */
		getNode:function(e){
				if(e&&(e.nodeType||e.item)){
					return e;
				}
				if(typeof e==='string'){
					return this.getById(e);
				}
			return null;
		},

		/**
		 * 删除对象
		 * @param {String|HTMLElement} el 
		 */
		removeElement: function(el){
			if(typeof(el)=="string"){
				el=QZFL.dom.getById(el);
			}
			if (!el) {
				return;
			}
			if (el.removeNode) {
				el.removeNode(true);
			}else {
				if(el.childNodes.length>0){
					for (var ii = el.childNodes.length - 1; ii >= 0; ii--) {
						HQ.dom.removeElement(el.childNodes[ii]);
					}
				}
				if(el.parentNode){
					el.parentNode.removeChild(el);
				}
			}
			el = null;
			return null;
		},
		
		/**
		 * 通过className来递归查询dom
		 * @param {String|HTMLElement} el 对象id或者dom
		 */
		searchElementByClassName: function(el,className){
			el = this.get(el);
			if (!el) {return null}

			var re = HQ.css.getClassRegEx(className);
		    while (el) {
		       if (re.test(el.className)) {
		            return el;
		       }
		       el = el.parentNode;
		    }
		    return null;
		},
		/**
		 * 获取classname的DOM数组
		 * @param {String} className 对象的className
		 * @param {String|HTMLElement} tag 对象的HTMLElement
		 * @param {Bool} insertFirst 是否插入容器的第一个位置
		 * @param {Object} attributes 对象属性列表，例如 {id:"newDom1",style:"color:#000"}
		 */
		getElementsByClassName:function(className,tag,root){
			tag=tag||'*';
			root=(root)?this.get(root):null||document;
			if(!root){
				return[];
			}
			var nodes=[],
			elements=root.getElementsByTagName(tag),
			re=QZFL.css.getClassRegEx(className);
			for(var i=0,len=elements.length;i<len;++i){
				if(re.test(elements[i].className)){
					nodes[nodes.length]=elements[i];
				}
			}
			return nodes;
		} ,
		
		/**
		 * 定点创建Dom对象
		 * @param {String} tagName 创建对象的TagName
		 * @param {String|HTMLElement} el 对象id或则dom
		 * @param {Bool} insertFirst 是否插入容器的第一个位置
		 * @param {Object} attributes 对象属性列表，例如 {id:"newDom1",style:"color:#000"}
		 */
		createElementIn: function(tagName,el,insertFirst,attributes){
			tagName = tagName || "div";
			el = this.get(el) || document.body;

			var _doc = el.ownerDocument;
			var _e = _doc.createElement(tagName);
			
			//设置Element属性
			if (attributes) {
				for (var k in attributes){
					if (/class/.test(k)) {
						_e.className = attributes[k];
					}else if (/style/.test(k)){
						_e.style.cssText = attributes[k];
					}else{
						_e[k] = attributes[k];
					}
				}
			}
			
	        if (insertFirst) {
	            el.insertBefore(_e,el.firstChild);
	        }else{
	            el.appendChild(_e);
	        }
	        return _e;
		},
		
		/**
		 * 获取对象渲染后的样式规则
		 * @param {String|HTMLElement} el 对象id或则dom
		 * @param {String} property 样式规则
		 * @return 样式值
		 */
		getStyle: function(el,property){
			el = this.get(el);
			
			var w3cMode = document.defaultView && document.defaultView.getComputedStyle;
			var value = "";
			switch(property){
				case "float":
					property = w3cMode?"cssFloat":"styleFloat";
					break;
				case "opacity":
					if (!w3cMode) { //IE Mode
						var val = 100;
	                    try {
	                        val = el.filters['DXImageTransform.Microsoft.Alpha'].opacity;
	                    } catch(e) {
	                        try { 
	                            val = el.filters('alpha').opacity;
	                        } catch(e) {}
	                    }
	                    return val / 100;
					}
			}
			
			if (w3cMode) {
				var computed = document.defaultView.getComputedStyle(el, '');
				if (computed) {value = computed[property];}
 				return (value || el.style[property]);
			}else{
				return (el.currentStyle[property] || el.style[property]);
			}
		},

		/**
		 * 建立有name属性的element
		 * @param {String} type node的tagName
		 * @param {String} name name属性值
		 * @param {object} doc document
		 * @return {Object} 结果element
		 */
		createNamedElement: function(type, name, doc){
			doc = doc || document;
			var element;
			try {
				element = doc.createElement('<' + type + ' name="' + name + '">');
			} catch (ignore) {
			}
			if (!element || !element.name) {
				element = doc.createElement(type);
				element.name = name;
			}
			return element;
		},
		
		/**
		 * 设置样式规则
		 * @param {String|HTMLElement} el 对象id或则dom
		 * @param {String} property 样式规则
		 * @return 成功返回 true
		 */
		setStyle: function(el,property,value){
			el = this.get(el);
			if (!el) {return false;}
			var w3cMode = document.defaultView && document.defaultView.getComputedStyle;

			switch(property){
				case "float":
					property = w3cMode?"cssFloat":"styleFloat";
				case "opacity":
 					if (!w3cMode) {
						if (value >= 1){
                    		el.style.filter = "";
                    		return;
                    	}
                        el.style.filter = 'alpha(opacity=' + (value*100)+ ')';
						return true;
                    }else{
                    	el.style[property]  = "";
                    }
				default:
					if (typeof el.style[property] == "undefined") {return false}
					el.style[property] = value;
					return true;
			}
		},
		
		/**
		 * 获取对象坐标
		 * @param {HTMLElement} el
		 * @return 对象
		 * @type Array
		 */ 
		getPosition: function(el){
		   	var xy = HQ.dom.getXY(el);
			var size = HQ.dom.getSize(el);
		    return {"top":xy[1],"left":xy[0],"width":size[0],"height":size[1]};
		},
		
		/**
		 * 获取对象坐标
		 * @param {HTMLElement} el
		 * @return Array [top,left]
		 * @type Array
		 */ 
		getXY: function(el){
		    var _t=0;
		    var _l=0;
			if (document.documentElement.getBoundingClientRect){ //顶IE的这个属性，获取对象到可视范围的距离
                var box = el.getBoundingClientRect();
				var oDoc = el.ownerDocument;
				_t = box.top - 2 + this.getScrollTop(oDoc);
				_l = box.left - 2 + this.getScrollLeft(oDoc);
			}else{
			    while (el.offsetParent) {
			        _t += el.offsetTop;
			        _l += el.offsetLeft;
			        el = el.offsetParent;
			    }
			}
		    return [_l,_t];
		},

		/**
		 * 获取对象尺寸
		 * @param {HTMLElement} el
		 * @return Array [width,height]
		 * @type Array
		 */ 
		getSize: function(el){
		    var _w = el.offsetWidth;
		    var _h = el.offsetHeight;
		    return [_w,_h];
		},
		
		/**
		 * 设置dom坐标
		 * @param {HTMLElement} el
		 * @param {string|number} x 横坐标
		 * @param {string|number} y 纵坐标
		 */
		setXY: function(el,x,y){
			el = this.get(el);
			var _ml = parseInt(this.getStyle(el,"marginLeft")) || 0;
			var _mt = parseInt(this.getStyle(el,"marginTop")) || 0;
			
			this.setStyle(el,"left",parseInt(x) - _ml + "px");
			this.setStyle(el,"top",parseInt(y) - _mt + "px");
		},
		
		/**
		 * 获取对象scrollLeft的值
		 * @param {Object} doc document
		 */
		getScrollLeft: function(doc){
            doc = doc || document;
            return Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft);
		},
		
		/**
		 * 获取对象的scrollTop的值
		 * @param {Object} doc document
		 */
		getScrollTop: function(doc){
            doc = doc || document;
            return Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
		},
		
		/**
		 * 获取对象scrollHeight的值
		 * @param {Object} doc document
		 */
		getScrollHeight: function(doc){
            doc = doc || document;
            return Math.max(doc.documentElement.scrollHeight, doc.body.scrollHeight);
		},
		
		/**
		 * 获取对象的scrollWidth的值
		 * @param {Object} doc document
		 */
		getScrollWidth: function(doc){
            doc = doc || document;
            return Math.max(doc.documentElement.scrollWidth, doc.body.scrollWidth);
		},
		
		/**
		 * 设置对象scrollLeft的值
		 * @param {number} value scroll left的修改值
		 * @param {Object} doc document
		 */

		setScrollLeft: function(value,doc){
            doc = doc || document;
            doc[doc.compatMode == "CSS1Compat"?"documentElement":"body"].scrollLeft = value;
		},
		
		/**
		 * 设置对象的scrollTop的值
		 * @param {number} value scroll top的修改值
		 * @param {Object} doc document
		 */
		setScrollTop: function(value,doc){
            doc = doc || document;
            doc[doc.compatMode == "CSS1Compat"?"documentElement":"body"].scrollTop = value;
		},
		
		/**
		 * 获取对象的可视区域高度
		 * @param {Object} doc document
		 */
		getClientHeight: function(doc){
			 doc = doc || document;
			 return  doc.compatMode == "CSS1Compat"?doc.documentElement.clientHeight:doc.body.clientHeight;
		},
		
		/**
		 * 获取对象的可视区域宽度
		 * @param {Object} doc document
		 */
		getClientWidth: function(doc){
			 doc = doc || document;
			 return  doc.compatMode == "CSS1Compat"?doc.documentElement.clientWidth:doc.body.clientWidth;
		},
		/**
		 * 设置dom尺寸
		 * @param {HTMLElement} el
		 * @param {string|number} width 宽度
		 * @param {string|number} height 高度
		 */
		setSize: function(el,width,height){
			el = this.get(el);
			
			var _wFix = /\d+([a-z%]+)/i.exec(width);
			_wFix = _wFix?_wFix[1]:"";
			var _hFix = /\d+([a-z%]+)/i.exec(height);
			_hFix = _hFix?_hFix[1]:"";
			
			this.setStyle(el,"width",(!width || width<0 || /auto/i.test(width))?"auto":(parseInt(width) + (_wFix || "px")));
			this.setStyle(el,"height",(!height || height<0 || /auto/i.test(height))?"auto":(parseInt(height) + (_hFix || "px")));
		},
		
		/**
		 * 获取document的window对象
		 * @param {Document} doc document对象
		 * @return {Window} 返回window对象
		 */
		getDocumentWindow: function(doc){
			_doc = doc || document;
			return _doc.parentWindow || _doc.defaultView;
		},
		
		XMLselectSingleNode:function(o,xpath){
			var x=HQ.dom.XMLselectNodes(o,xpath)
			if(!x || x.length<1) return null;
			return x[0];
		},
		
		XMLselectNodes:function(o,xpath){
			var xpe = new XPathEvaluator();
			var nsResolver = xpe.createNSResolver(o.ownerDocument == null ?
				o.documentElement : o.ownerDocument.documentElement);
			var result = xpe.evaluate(xpath, o, nsResolver, 0, null);
			var found = [];
			var res;
			while (res = result.iterateNext()){
				found.push(res);	
			}
			return found;
		}
	};

var _CN = HQ.dom.createNamedElement;
var $ = HQ.dom.getById;
var removeNode=HQ.dom.removeElement;